import base64
import os
import json
import tkinter
import time
from tkinter import *


def get_text(entryn):
    return entryn.get()

# json解码
def dict_b64_str(b64_str1):
    dict_b64_str2 = json.loads(b64_str1)
    return dict_b64_str2

# 转换成 base64 编码
def b64_str(str1, encode_type='utf-8'):
    b64_str = base64.b64encode(str1.encode(encode_type))
    b64_str1 = b64_str.decode(encode_type)
    return b64_str1

# base64解码
def decode_base64_str(dict_b64_str, encode_type='utf-8'):
    dict_information = {}
    for key_b64_str in dict_b64_str:
        b64_passwor = dict_b64_str[key_b64_str]
        print(b64_passwor)
        decoded_b64_username = base64.b64decode(key_b64_str).decode(encode_type)
        decoded_b64_password = base64.b64decode(b64_passwor).decode(encode_type)
        dict_information[decoded_b64_username] = decoded_b64_password
        return dict_information

class Admin_register():

    def __init__(self,username,password):
        self.username=username
        self.password=password

    def record(self):
        pword = r'D:\admine.txt'
        if not os.path.exists(pword):
            f = open(pword,'w')
            f.close()

        with open(pword,'r',encoding='utf8') as  file:
            reader = file.read()
            if reader == '':
                files={}
            else:
                files = json.loads(reader)

        with open(r'D:\admine.txt','w',encoding='utf8') as f:
            files[b64_str(self.username)] = b64_str(self.password)
            files = json.dumps(files)
            f.write(files)

class Students_Register():

    def __init__(self,username,password):
        self.username=username
        self.password=password

    def record(self):
        pword = r'D:\students.txt'
        if not os.path.exists(pword):
            f = open(pword,'w')
            f.close()

        with open(pword,'r',encoding='utf8') as  file:
            reader = file.read()
            if reader == '':
                files={}
            else:
                files = json.loads(reader)

        with open(r'D:\students.txt','w',encoding='utf8') as f:
            files[b64_str(self.username)] = b64_str(self.password)
            files = json.dumps(files)
            f.write(files)

def reader_admine():
    with open(r'D:\admine.txt', 'r', encoding='utf8') as file:
        b64_reader = file.read()
        dict_b64_reader = dict_b64_str(b64_reader)
        reader = decode_base64_str(dict_b64_reader)
        return reader

def reader_students():
    with open(r'D:\students.txt', 'r', encoding='utf8') as file:
        b64_reader = file.read()
        dict_b64_reader = dict_b64_str(b64_reader)
        reader = decode_base64_str(dict_b64_reader)
        return reader

class Record_students_information():

    def __init__(self,students=[]):
        self.students=students


    def record(self,student_id,clss,name,age,gender):
        sid_value_information = {'班级':clss,'姓名':name,'年龄':age,'性别':gender}

        pword = r'D:\students_information.txt'
        if not os.path.exists(pword):
            f = open(pword,'w')
            f.close()

        with open(pword,'r',encoding='utf8') as  file:
            reader = file.read()
            if reader == '':
                files={}
            else:
                files = json.loads(reader)
        with open(r'D:\students_information.txt','w',encoding='utf8') as f:
            files[student_id] = sid_value_information
            print(files)
            files = json.dumps(files)
            f.write(files)

    def reader_students_information(self):
        with open(r'D:\students_information.txt', 'r', encoding='utf8') as file:
            reader = file.read()
            information = json.loads(reader)
            return information

    def rem_students_information(self,sid):
        with open(r'D:\students_information.txt', 'r', encoding='utf8') as file:
            file_reader = file.read()
            files = json.loads(file_reader)
        with open(r'D:\students_information.txt', 'w', encoding='utf8') as f:
            del files[sid]
            files = json.dumps(files)
            f.write(files)

    def change(self,sid,clas):
        with open(r'D:\students_information.txt', 'r', encoding='utf8') as file:
            file_reader = file.read()
            files = json.loads(file_reader)
        with open(r'D:\students_information.txt', 'w', encoding='utf8') as f:
            files[sid]['班级'] = clas
            files = json.dumps(files)
            f.write(files)




class Sorted_information():

    def __init__(self,students=[]):
        self.students=students


    def record(self,student_id,clss,name,age,gender):

        clas_value_information = {'学号':student_id,'姓名':name,'年龄':age,'性别':gender}
        self.students.append(clas_value_information)

        pword = r'D:\sorted_students_information.txt'
        if not os.path.exists(pword):
            f = open(pword,'w')
            f.close()

        with open(pword,'r',encoding='utf8') as  file:
            reader = file.read()
            if reader == '':
                files = {}
                files[clss] = self.students
                with open(pword,'w',encoding='utf8') as  f:
                    information = json.dumps(files)
                    f.write(information)
            else:
                files = json.loads(reader)
        with open(pword,'w',encoding='utf8') as  f:
            if clss in files:
                files[clss] = self.students
                information = json.dumps(files)
                f.write(information)

            else:
                self.students=[]
                self.students.append(clas_value_information)
                files[clss] = self.students

                information = json.dumps(files)
                f.write(information)


    def reader_sorted_information(self):
        with open(r'D:\sorted_students_information.txt', 'r', encoding='utf8') as file:
            reader = file.read()

            information = json.loads(reader)

            return information

    def rem_sorted_information(self,clss,sid):
        information = self.reader_sorted_information()
        print('---------->',information)
        # with open(r'D:\sorted_students_information.txt', 'w', encoding='utf8') as f:
        list_sid = []
        for i in information[clss]:
            list_sid.append(i['学号'])
        print(list_sid)
        if sid in list_sid:
            i = len(information[clss])-1
            while i in range(len(information[clss])):
                if sid == information[clss][i]['学号']:
                    return 'true'
                else:
                    i -= 1
        else:
            return 'false'




    def change(self,sid,source_clas,newclas):
        with open(r'D:\sorted_students_information.txt', 'r', encoding='utf8') as file:
            file_reader = file.read()
            files = json.loads(file_reader)
        with open(r'D:\sorted_students_information.txt', 'w', encoding='utf8') as f:
            for source in files[source_clas]:
                if sid == source['学号']:
                    files[newclas].append(source)
                    files[source_clas].remove(source)
                    files = json.dumps(files)
                    f.write(files)






